#pragma once

#include <string.h>
#include <boost/cstdint.hpp>
#include <boost/detail/endian.hpp>
#include "hash.hpp"

namespace alpha { namespace crypto {

namespace detail {

// modified from Steve Reid's <steve@edmweb.com>
// C implementation placed in the public domain

struct sha1_traits
{
	enum {digest_length = 20};

	typedef struct {
		boost::uint32_t state[5];
		boost::uint32_t count[2];
		boost::uint8_t buffer[64];
	} context_type;

	static void init(context_type *context)
	{
		context->state[0] = 0x67452301;
		context->state[1] = 0xEFCDAB89;
		context->state[2] = 0x98BADCFE;
		context->state[3] = 0x10325476;
		context->state[4] = 0xC3D2E1F0;
		context->count[0] = context->count[1] = 0;
	}

	static void update(context_type *context, boost::uint8_t const *data, boost::uint32_t len)
	{
		boost::uint32_t i, j = (context->count[0] >> 3) & 63;
		if ((context->count[0] += len << 3) < (len << 3))
			context->count[1]++;
		context->count[1] += (len >> 29);
		if ((j + len) > 63)
		{
			memcpy(&context->buffer[j], data, (i = 64-j));
			transform(context->state, context->buffer);
			for ( ; i + 63 < len; i += 64)
				transform(context->state, &data[i]);
			j = 0;
		}
		else
			i = 0;
		memcpy(&context->buffer[j], &data[i], len - i);
	}

	static void final(boost::uint8_t *digest, context_type *context)
	{
		boost::uint8_t finalcount[8];
		for (unsigned i = 0; i < 8; i++)
			finalcount[i] = (boost::uint8_t)((context->count[(i >= 4 ? 0 : 1)] >> ((3-(i & 3)) * 8) ) & 255);  // Endian independent
		update(context, (boost::uint8_t *)"\200", 1);
		while ((context->count[0] & 504) != 448)
			update(context, (boost::uint8_t *)"\0", 1);
		update(context, finalcount, 8);  // Should cause a transform()
		for (unsigned i = 0; i < digest_length; i++)
			*digest++ = (boost::uint8_t)((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
	}

private:

	inline static boost::uint32_t rol(boost::uint32_t value, boost::uint32_t bits)
	{
		return (value << bits) | (value >> (32 - bits));
	}

	typedef union {
		boost::uint8_t c[64];
		boost::uint32_t l[16];
		// blk0() and blk() perform the initial expand.
		inline boost::uint32_t blk(boost::uint32_t i)
		{
			return (this->l[i&15] = rol(this->l[(i+13)&15]^this->l[(i+8)&15]^this->l[(i+2)&15]^this->l[i&15],1));
		}
		inline boost::uint32_t blk0(boost::uint32_t i)
		{
#ifdef BOOST_LITTLE_ENDIAN
			return (this->l[i] = (rol(this->l[i],24)&0xFF00FF00) | (rol(this->l[i],8)&0x00FF00FF));
#elif defined(BOOST_BIG_ENDIAN)
			return this->l[i];
#else
#error unsupported endian
#endif
		}
		// (R0+R1), R2, R3, R4 are the different operations used in SHA1
		inline void R0(boost::uint32_t v, boost::uint32_t &w, boost::uint32_t x, boost::uint32_t y, boost::uint32_t &z, boost::uint32_t i)
		{
			z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
		}
		inline void R1(boost::uint32_t v, boost::uint32_t &w, boost::uint32_t x, boost::uint32_t y, boost::uint32_t &z, boost::uint32_t i)
		{
			z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
		}
		inline void R2(boost::uint32_t v, boost::uint32_t &w, boost::uint32_t x, boost::uint32_t y, boost::uint32_t &z, boost::uint32_t i)
		{
			z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
		}
		inline void R3(boost::uint32_t v, boost::uint32_t &w, boost::uint32_t x, boost::uint32_t y, boost::uint32_t &z, boost::uint32_t i)
		{
			z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
		}
		inline void R4(boost::uint32_t v, boost::uint32_t &w, boost::uint32_t x, boost::uint32_t y, boost::uint32_t &z, boost::uint32_t i)
		{
			z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
		}
	} CHAR64LONG16;

	static void transform(boost::uint32_t state[5], boost::uint8_t const buffer[64])
	{
		boost::uint32_t a, b, c, d, e;
		boost::uint8_t workspace[64];
		CHAR64LONG16* blk = (CHAR64LONG16*)workspace;
		memcpy(blk, buffer, 64);
		// Copy context->state[] to working vars
		a = state[0];
		b = state[1];
		c = state[2];
		d = state[3];
		e = state[4];
		// 4 rounds of 20 operations each. Loop unrolled.
		blk->R0(a,b,c,d,e, 0); blk->R0(e,a,b,c,d, 1); blk->R0(d,e,a,b,c, 2); blk->R0(c,d,e,a,b, 3);
		blk->R0(b,c,d,e,a, 4); blk->R0(a,b,c,d,e, 5); blk->R0(e,a,b,c,d, 6); blk->R0(d,e,a,b,c, 7);
		blk->R0(c,d,e,a,b, 8); blk->R0(b,c,d,e,a, 9); blk->R0(a,b,c,d,e,10); blk->R0(e,a,b,c,d,11);
		blk->R0(d,e,a,b,c,12); blk->R0(c,d,e,a,b,13); blk->R0(b,c,d,e,a,14); blk->R0(a,b,c,d,e,15);
		blk->R1(e,a,b,c,d,16); blk->R1(d,e,a,b,c,17); blk->R1(c,d,e,a,b,18); blk->R1(b,c,d,e,a,19);
		blk->R2(a,b,c,d,e,20); blk->R2(e,a,b,c,d,21); blk->R2(d,e,a,b,c,22); blk->R2(c,d,e,a,b,23);
		blk->R2(b,c,d,e,a,24); blk->R2(a,b,c,d,e,25); blk->R2(e,a,b,c,d,26); blk->R2(d,e,a,b,c,27);
		blk->R2(c,d,e,a,b,28); blk->R2(b,c,d,e,a,29); blk->R2(a,b,c,d,e,30); blk->R2(e,a,b,c,d,31);
		blk->R2(d,e,a,b,c,32); blk->R2(c,d,e,a,b,33); blk->R2(b,c,d,e,a,34); blk->R2(a,b,c,d,e,35);
		blk->R2(e,a,b,c,d,36); blk->R2(d,e,a,b,c,37); blk->R2(c,d,e,a,b,38); blk->R2(b,c,d,e,a,39);
		blk->R3(a,b,c,d,e,40); blk->R3(e,a,b,c,d,41); blk->R3(d,e,a,b,c,42); blk->R3(c,d,e,a,b,43);
		blk->R3(b,c,d,e,a,44); blk->R3(a,b,c,d,e,45); blk->R3(e,a,b,c,d,46); blk->R3(d,e,a,b,c,47);
		blk->R3(c,d,e,a,b,48); blk->R3(b,c,d,e,a,49); blk->R3(a,b,c,d,e,50); blk->R3(e,a,b,c,d,51);
		blk->R3(d,e,a,b,c,52); blk->R3(c,d,e,a,b,53); blk->R3(b,c,d,e,a,54); blk->R3(a,b,c,d,e,55);
		blk->R3(e,a,b,c,d,56); blk->R3(d,e,a,b,c,57); blk->R3(c,d,e,a,b,58); blk->R3(b,c,d,e,a,59);
		blk->R4(a,b,c,d,e,60); blk->R4(e,a,b,c,d,61); blk->R4(d,e,a,b,c,62); blk->R4(c,d,e,a,b,63);
		blk->R4(b,c,d,e,a,64); blk->R4(a,b,c,d,e,65); blk->R4(e,a,b,c,d,66); blk->R4(d,e,a,b,c,67);
		blk->R4(c,d,e,a,b,68); blk->R4(b,c,d,e,a,69); blk->R4(a,b,c,d,e,70); blk->R4(e,a,b,c,d,71);
		blk->R4(d,e,a,b,c,72); blk->R4(c,d,e,a,b,73); blk->R4(b,c,d,e,a,74); blk->R4(a,b,c,d,e,75);
		blk->R4(e,a,b,c,d,76); blk->R4(d,e,a,b,c,77); blk->R4(c,d,e,a,b,78); blk->R4(b,c,d,e,a,79);
		// Add the working vars back into context.state[]
		state[0] += a;
		state[1] += b;
		state[2] += c;
		state[3] += d;
		state[4] += e;
	}
};

} // namespace detail

typedef basic_hash<detail::sha1_traits> sha1;

}} // namespace alpha::crypto
